package com.dotflat.fin.service.impl;

import com.dotflat.fin.PO.ProductSolutionItemPO;
import com.dotflat.fin.PO.ProductSolutionPO;
import com.dotflat.fin.cnst.ProductTypeEnum;
import com.dotflat.fin.domain.ProductSolution;
import com.dotflat.fin.domain.ProductSolutionItem;
import com.dotflat.fin.repository.FinPayAccountRepository;
import com.dotflat.fin.repository.ProductSolutionItemRepository;
import com.dotflat.fin.repository.ProductSolutionRepository;
import com.dotflat.fin.service.ProductSolutionService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.utils.CopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单信息service
 *
 * @author ：Ron
 * @date ：Created in 2020/10/30 13:06
 */
@Service
@Slf4j
public class ProductSolutionServiceImpl
        extends AreableObjectBaseServiceImpl<ProductSolution, ProductSolutionRepository>
        implements ProductSolutionService {

    @Override
    @Autowired
    public void setBaseRepository(ProductSolutionRepository baseRepository) {
        super.setBaseRepository(baseRepository);
    }

    @Autowired
    private ProductSolutionItemRepository itemRepository;

    @Autowired
    private FinPayAccountRepository payAccountRepository;

    @Override
    @Transactional(rollbackOn = RuntimeException.class)
    public ProductSolution add(String areaId, String staffd, AreableObjectBasePO param){


        if (!(param instanceof ProductSolutionPO)){
            return super.add(areaId, staffd, param);
        }

        ProductSolutionPO po = (ProductSolutionPO)param;

        if(StringUtils.isBlank(po.getId())) {
            ProductSolution solution = baseRepository.findFirstByAreaIdAndName(areaId, po.getName());

            if (solution != null) {
                throw new BusinessException(ErrorCode.MOBILE_IS_EXIST, "同名对象已经存在");
            }
        }

        ProductSolution solution = super.add(areaId, staffd, param);

        //保存item
        if (po.getProducts() != null && po.getProducts().length > 0) {

            for (ProductSolutionItemPO o : po.getProducts()) {
                o.setCreatedBy(staffd);
                o.setAreaId(areaId);
                o.setSolutionId(solution.getId());
            }

            List<ProductSolutionItemPO> poItems = Arrays.asList(po.getProducts());

            //TODO：判断收费方案的类型，过滤掉定金
            long djCount = poItems.stream().filter(o->o.getProductType() == ProductTypeEnum.DEPOSIT.getCode()).count();
            if (djCount > 0 && djCount != poItems.size()){
                throw new BusinessException(ErrorCode.CONTRACT_MONEY_ERROR, "定金只能单独收取");
            }

            List<ProductSolutionItem>  productSolutionItemList = poItems.stream().map(o->{
                ProductSolutionItem p = new ProductSolutionItem();
                CopyUtils.copyProperties(o, p);
                return p;
            }).collect(Collectors.toList());

            List<ProductSolutionItem> items = itemRepository.saveAll(productSolutionItemList);

            solution.setProducts(items);

            //删除掉旧的
            List<String> ids = items.stream().map(o->o.getId()).collect(Collectors.toList());
            int count = itemRepository.deleteAllByAreaIdAndSolutionIdAndIdNotIn(areaId, solution.getId(), ids);

            log.info("delete item count :::" + count);
        }

        return solution;
    }

}
